package com.zh.manage.log;

import com.google.common.collect.Maps;
import com.zh.manage.service.impl.CardBinServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;


@Aspect
@Component
public class ManageRecordAop {
    private static final Logger log = LoggerFactory.getLogger(ManageRecordAop.class);

    /**
     * Controller切点
     */
    @Pointcut("execution(* com.zh.manage.controller..*(..))" +
            " && @annotation(io.swagger.annotations.ApiOperation)" +
            " && !@annotation(org.springframework.web.bind.annotation.GetMapping)" )
    public void recordControllerLog() { }

    /**
     * @Description: AOP拦截记录操作日志
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/15
     */
    @AfterReturning(value = "recordControllerLog()" , returning = "result" )
    public void After(JoinPoint jp, Object result) {
        Object targetObj = jp.getTarget();
        /*if ((targetObj instanceof CardBinServiceImpl)) {
            return;
        }*/

        MethodSignature methodSignature = (MethodSignature) jp.getSignature();
        Method method = methodSignature.getMethod();
        try {
            // *========操作日志=========*//
            /*Auth principal = (Auth) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            ChargeRecord chargeRecord = new ChargeRecord();
            Gson gson = new GsonBuilder().create();*/
            /* 获取请求参数 */
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            Enumeration<String> enumeration = request.getParameterNames();
            Map<String,String> parameterMap = Maps.newHashMap();
            while (enumeration.hasMoreElements()){
                String parameter = enumeration.nextElement();
                parameterMap.put(parameter,request.getParameter(parameter));
            }

            /* 获取用户ip */
            /*OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder
                    .getContext().getAuthentication().getDetails();*/

            /* 获取ApiOperation注解设置的notes作为操作内容 */
            if (method.isAnnotationPresent(ApiOperation.class)) {
                ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
                log.info("请求记录:{}.{},[ {} ]" , jp.getTarget().getClass().getName(), method.getName(), apiOperation.notes());
//                chargeRecord.setContent(apiOperation.notes());
            } else {
//                chargeRecord.setContent(method.getName());
            }


        } catch (Exception e) {
            log.error("异常信息:{}" , e.getMessage());
        }
    }
}
